package com.zmx.promote.service;

import com.zmx.common.common.service.BaseService;
import com.zmx.dto.PromoteActiveDto;
import com.zmx.promote.entity.PromoteActive;
import com.zmx.promote.repository.PromoteActiveRepository;
import com.zmx.promote.service.mapper.PromoteActiveMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhaomxr
 */
@Service
public class PromoteActiveService extends BaseService<PromoteActive, PromoteActiveDto, PromoteActiveRepository, PromoteActiveMapper> {

    public Page<PromoteActiveDto> getActivePage(Integer pageIndex, Integer pageSize) {
        Map<String, Object> params = new HashMap<>(4);
        params.put("isVaild", 1);
        params.put("endDate_le", new Date());
        params.put("ranking_sort", Sort.Direction.DESC);

        return this.getAll(params, pageIndex, pageSize, true);
    }

    public List<PromoteActiveDto> getSkillActiveList(int type) {
        Map params = new HashMap<String, Object>(3);
        params.put("isVaild", 1);
        params.put("type", type);
        params.put("ranking_sort", Sort.Direction.DESC);

        List<PromoteActiveDto> promoteActiveDtoList = null;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        try {
            List<PromoteActive> promoteActiveList = this.getAll(params);
            promoteActiveDtoList = entityToDto(promoteActiveList);
            for (PromoteActiveDto activeDTO : promoteActiveDtoList) {
                String createDateStr = df.format(activeDTO.getBeginDate());
                String endDateStr = df.format(activeDTO.getEndDate());
                String now = df.format(new Date());
                createDateStr = createDateStr.substring(createDateStr.length() - 9, createDateStr.length() - 4);
                endDateStr = endDateStr.substring(endDateStr.length() - 9, endDateStr.length() - 4);
                activeDTO.setBeginDateStr(createDateStr);
                activeDTO.setEndSateStr(endDateStr);
                DateFormat df1 = new SimpleDateFormat("HH:mm:ss");
                Date dtbegin = df1.parse(df.format(activeDTO.getBeginDate()).substring(df.format(activeDTO.getBeginDate()).length() - 9));
                Date dtend = df1.parse(df.format(activeDTO.getEndDate()).substring(df.format(activeDTO.getEndDate()).length() - 9));
                Date dt2 = df1.parse(now.substring(now.length() - 9));

                if (dtbegin.getTime() > dt2.getTime() && dtend.getTime() > dt2.getTime()) {
                    activeDTO.setActiveStatus("未开始");
                } else if (dtbegin.getTime() < dt2.getTime() && dtend.getTime() > dt2.getTime()) {
                    activeDTO.setActiveStatus("进行中");
                } else if (dtend.getTime() < dt2.getTime()) {
                    activeDTO.setActiveStatus("已结束");
                }
                //将开始和结束时间转为时间戳格式
                activeDTO.setBeginDateL(activeDTO.getBeginDate());
                activeDTO.setEndDateL(activeDTO.getEndDate());
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return promoteActiveDtoList;
    }
}
